解决PowerShell中conda activate指令无效的问题

您所在的位置:网站首页 powershell profile文件 解决PowerShell中conda activate指令无效的问题

解决PowerShell中conda activate指令无效的问题

2023-05-27 02:53| 来源: 网络整理| 查看: 265

解决步骤(省流不想看版):

打开PowerShell终端键入指令conda init powershell,初始化conda键入指令Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine,修改PowerShell限制策略重启PowerShell,检查conda activate是否生效 问题描述

Anaconda已正确安装,但在PowerShell中出现以下问题(CMD可能没事):

使用conda activate指令激活环境不起作用输入提示的"PS"前不显示当前环境,例如正确应该显示(base) PS C:\Users\Administrator>:

如下述例子,用conda activate pytorch激活pytorch环境,然后用conda info --envs显示当前环境,发现仍然为base,未激活成功;且输入提示的"PS"前不显示(base)。

PS C:\Users\Administrator> conda info --envs # conda environments: # base * C:\Users\Administrator\anaconda3 pytorch C:\Users\Administrator\anaconda3\envs\pytorch PS C:\Users\Administrator> conda activate pytorch PS C:\Users\Administrator> conda info --envs # conda environments: # base * C:\Users\Administrator\anaconda3 pytorch C:\Users\Administrator\anaconda3\envs\pytorch PS C:\Users\Administrator> 问题原因

原因:conda在PowerShell中未正确配置,导致conda activate不能用。

其实,存在这种问题时,conda activate是会有报错的,但不知为何,在PowerShell中这个报错没有出现。如果你安装了git,可以在其自带的git bash中输入conda activate your_env,会出现如下提示:

$ conda activate pytorch CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'. If using 'conda activate' from a batch script, change your invocation to 'CALL conda.bat activate'. To initialize your shell, run $ conda init Currently supported shells are: - bash - cmd.exe - fish - tcsh - xonsh - zsh - powershell See 'conda init --help' for more information and options. IMPORTANT: You may need to close and restart your shell after running 'conda init'. 解决步骤 第一步:初始化conda

根据上一节的报错提示,我们用conda init来初始化conda环境:

conda init powershell

该命令的执行结果如下:

PS C:\Users\Administrator> conda init powershell no change C:\Users\Administrator\anaconda3\Scripts\conda.exe no change C:\Users\Administrator\anaconda3\Scripts\conda-env.exe no change C:\Users\Administrator\anaconda3\Scripts\conda-script.py no change C:\Users\Administrator\anaconda3\Scripts\conda-env-script.py no change C:\Users\Administrator\anaconda3\condabin\conda.bat no change C:\Users\Administrator\anaconda3\Library\bin\conda.bat no change C:\Users\Administrator\anaconda3\condabin\_conda_activate.bat no change C:\Users\Administrator\anaconda3\condabin\rename_tmp.bat no change C:\Users\Administrator\anaconda3\condabin\conda_auto_activate.bat no change C:\Users\Administrator\anaconda3\condabin\conda_hook.bat no change C:\Users\Administrator\anaconda3\Scripts\activate.bat no change C:\Users\Administrator\anaconda3\condabin\activate.bat no change C:\Users\Administrator\anaconda3\condabin\deactivate.bat no change C:\Users\Administrator\anaconda3\Scripts\activate no change C:\Users\Administrator\anaconda3\Scripts\deactivate no change C:\Users\Administrator\anaconda3\etc\profile.d\conda.sh no change C:\Users\Administrator\anaconda3\etc\fish\conf.d\conda.fish no change C:\Users\Administrator\anaconda3\shell\condabin\Conda.psm1 no change C:\Users\Administrator\anaconda3\shell\condabin\conda-hook.ps1 no change C:\Users\Administrator\anaconda3\Lib\site-packages\xontrib\conda.xsh no change C:\Users\Administrator\anaconda3\etc\profile.d\conda.csh modified C:\Users\Administrator\Documents\WindowsPowerShell\profile.ps1 ==> For changes to take effect, close and re-open your current shell.

然后,关闭并重新打开PowerShell,发现出现报红(而且conda acitvate还是无效):

Windows PowerShell 版权所有(C) Microsoft Corporation。保留所有权利。 安装最新的 PowerShell,了解新功能和改进!https://aka.ms/PSWindows . : 无法加载文件 C:\Users\Administrator\Documents\WindowsPowerShell\profile.ps1,因为在此系统上禁止运行脚本。有关详细信 息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。 所在位置 行:1 字符: 3 + . 'C:\Users\Administrator\Documents\WindowsPowerShell\profile.ps1' + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [],PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess PS C:\Users\Administrator>

这是由Windows客户端中PowerShell默认为限制(Restricted)的执行策略导致的,接下来将解决这个问题。

第二步:修改PowerShell限制策略

进入上述PowerShell报红中给出的网站,可以知道Windows客户端中PowerShell默认的限制策略为Restricted:

Restricted限制策略

下面将直接给出指令来修改该限制策略。关于详细的限制策略信息和修改方法,可以自行参阅报红中给出的网站(https:/go.microsoft.com/fwlink/?LinkID=135170)。

使用以下指令,将限制策略从Restricted改为RemoteSigned,范围为计算机上所有用户:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

再次重启PowerShell,发现conda activate已可以使用:

Windows PowerShell 版权所有(C) Microsoft Corporation。保留所有权利。 安装最新的 PowerShell,了解新功能和改进!https://aka.ms/PSWindows 加载个人及系统配置文件用了 1393 毫秒。 (base) PS C:\Users\Administrator> conda info --envs # conda environments: # base * C:\Users\Administrator\anaconda3 pytorch C:\Users\Administrator\anaconda3\envs\pytorch (base) PS C:\Users\Administrator> conda activate pytorch (pytorch) PS C:\Users\Administrator> conda info --envs # conda environments: # base C:\Users\Administrator\anaconda3 pytorch * C:\Users\Administrator\anaconda3\envs\pytorch (pytorch) PS C:\Users\Administrator>


【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3